import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/books.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/routes/routePages.dart';
import 'package:ichinesereader/themes/pageStyles.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/buttons/floatingActionButtonAddBooksToFolder.dart';
import 'package:ichinesereader/widgets/layouts/appdrawerStudent.dart';
import 'package:ichinesereader/widgets/layouts/customAppBar.dart';
import 'package:ichinesereader/widgets/module/leveledReadingBody.dart';
import 'package:ichinesereader/models/book/BookSearchDto.dart';
import 'package:ichinesereader/widgets/sidebars/studentSidebar.dart';

class LeveledReadings extends StatefulWidget {
  static const String routeName = '/student/leveledreadings';

  @override
  State<LeveledReadings> createState() => _LeveledReadingsState();
}

class _LeveledReadingsState extends State<LeveledReadings> {
  final box = Hive.box('appconfig');
  final BookSearchDto dto = new BookSearchDto();
  final BooksController _booksController = Get.find();

  @override
  Widget build(BuildContext context) {
    Log.debug('build student LeveledReadings page');
    _booksController.setBookSearchDto(dto);
    bool isStudent = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Student;
    final isSmallScreen =
        MediaQuery.of(context).size.width < Constants.DefinedScreenSmallSize;
    final isBigScreen =
        MediaQuery.of(context).size.width > Constants.DefinedScreenBigSize;
    int _selectedSidebarItem = RoutePages.studentPageItems
        .indexWhere((page) => page.route == Get.currentRoute);
    final PageStyles _pageStyles = PageStyles();

    return Scaffold(
      appBar: isSmallScreen ? CustomAppBar(title: 'Leveled Readings'.tr) : null,
      drawer: isSmallScreen ? StudentAppDrawer() : null,
      body: Container(
        decoration: _pageStyles.pageBoxDecoration(),
        child: Row(
          children: [
            Visibility(
                visible: !isSmallScreen,
                child: StudentSidebarX(
                    selectedIndex: _selectedSidebarItem,
                    isBigScreen: isBigScreen)),
            Expanded(
              child: isStudent
                  ? Container(
                      margin:
                          _pageStyles.mainContainerWithPaginationEdgeInsets(),
                      child: LeveledReadingBody())
                  : Text(''),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButtonAddBookToFolder(
          readingType: NanhaiBookReadingTypes.leveled),
    );
  }
}
